package com.project.finance.mobile.user.controller;

import com.mzlion.core.lang.StringUtils;
import com.project.finance.common.po.TbUsers;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.mobile.user.service.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Brendan Li
 * @description 登录处理请求
 * @Date: 2019/12/09/19:21
 */
@RestController
public class LoginController {

    private static final Logger logger= LogManager.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Result dealLogin(@RequestParam("phoneNumber") String phoneNum,
                            @RequestParam("password") String password){
        if (!StringUtils.isEmpty(phoneNum)) {
            if (!StringUtils.isEmpty(password)){
                Hash hash = new SimpleHash("MD5",password,RegisterController.salt,1024);
                String passwd = hash.toBase64();
                TbUsers user = loginService.getUser(phoneNum);
                if (user!=null){
                    if (passwd.equals(user.getPassword())){
                        return ResultGenerator.apiSuccess(user);
                    }
                    return ResultGenerator.apiError("登录失败");
                }
            }
            return ResultGenerator.apiError("密码为空");
        }
        return ResultGenerator.apiError("手机号为空");
    }
}
